﻿using System.Collections.Generic;
using System.Linq;
using Nydus.Model.Entities;
using Nydus.Model.Interfaces;

namespace Nydus.Model.Repositories
{
    /// <summary>
    /// Agents repository.
    /// </summary>
    public class AgentRepository : IEntityRepository<Agent>
    {
        private INydusDataContext context;

        /// <summary>
        /// Creates an instance of agent repository
        /// </summary>
        /// <param name="context">The data context</param>
        public AgentRepository(INydusDataContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// Creates an instance of agent repository
        /// </summary>
        /// <param name="uow">Unit of work object</param>
        public AgentRepository(UnitOfWork uow)
        {
            this.context = uow.Context;
        }

        /// <summary>
        /// Get an agent entity by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Agent GetById(int id)
        {
            return context.Agents.First(c => c.Id == id);
        }

        /// <summary>
        /// Adds a new agent.
        /// </summary>
        /// <param name="entity"></param>
        public void Add(Agent entity)
        {
            context.Agents.AddObject(entity);
        }

        /// <summary>
        /// Deletes the agent.
        /// </summary>
        /// <param name="entity"></param>
        public void Delete(Agent entity)
        {
            context.Agents.DeleteObject(entity);
        }

        /// <summary>
        /// Gets the list of all agents.
        /// </summary>
        public IList<Agent> All
        {
            get { return context.Agents.ToList(); }
        }

        /// <summary>
        /// Gets the list of active agents.
        /// </summary>
        public IList<Agent> Active
        {
            get { return context.Agents.Where(t => t.IsActive).ToList(); }
        }
    }
}
